package com.myzl.coal.client.parse;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.net.NetUtil;
import com.myzl.coal.client.CacheData;
import com.myzl.coal.domain.AlarmEvent;
import com.myzl.coal.domain.dto.RealTimeDataDTO;
import com.myzl.coal.mq.MqService;
import com.myzl.common.enums.CurrencyEnum;
import com.myzl.common.enums.DeviceTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author Administrator
 */
@Slf4j(topic = "admin-collect")
public abstract class CommonParseService implements CommonParseInterface{

    List<RealTimeDataDTO> realTimeDataList = new CopyOnWriteArrayList();
    List<AlarmEvent> alarmEvents = new CopyOnWriteArrayList<>();
    List<AlarmEvent> restore = new CopyOnWriteArrayList<>();


    /**
     * channelRead
     *
     * @param msg
     * @param deviceId
     * @return void
     * @author wzb
     * @date 2023/1/3
     * @description
     */
    public void channelRead(Object msg, Integer deviceId) {

    }

    /**
     * initConnect
     *
     * @param deviceId
     * @return void
     * @author wzb
     * @date 2023/1/3
     * @description 设备初始化
     */
    public void initConnect(Integer deviceId) {
    }


    /**
     * 增加上报时间存储记录
     *
     * @param deviceId,deviceTypeEnum
     * @return * @return null
     * @author wzb
     * @date 2023/7/24
     * @description
     */
    void reportData(Integer deviceId, Integer deviceType) {
        RealTimeDataDTO realTimeData = new RealTimeDataDTO();
        realTimeData.setDeviceId(deviceId);
        realTimeData.setDictValue(CurrencyEnum.REPORTING_TIME.getCode());
        realTimeData.setDeviceType(deviceType);
        realTimeData.setDataValue(DateUtil.now());
        realTimeDataList.add(realTimeData);
    }


    void addData(Integer deviceId, Integer deviceType,Integer dictValue,String dataValue) {
        RealTimeDataDTO realTimeDataDTO = new RealTimeDataDTO();
        realTimeDataDTO.setDeviceId(deviceId);
        realTimeDataDTO.setDictValue(dictValue);
        realTimeDataDTO.setDeviceType(deviceType);
        realTimeDataDTO.setDataValue(dataValue);
        realTimeDataDTO.setDataUnit(CacheData.getStringSysDictDataMap().get(dictValue+"").getValueUnit());
        realTimeDataList.add(realTimeDataDTO);
    }

    /**
     * 存储
     *
     * @param mqService
     * @return * @return null
     * @author wzb
     * @date 2023/6/16
     * @description
     */
    void storageData(MqService mqService, DeviceTypeEnum deviceTypeEnum) {
        if (!CollectionUtils.isEmpty(realTimeDataList)) {
            Integer deviceId = realTimeDataList.get(0).getDeviceId();
            Integer deviceType = realTimeDataList.get(0).getDeviceType();
            timeMap.put(realTimeDataList.get(0).getDeviceId(), LocalDateTime.now());
            if (deviceTypeEnum.getCode() != deviceType) {
                System.out.println("");
            }

            reportData(deviceId, deviceType);
            String address = NetUtil.getLocalhost().getHostAddress();
            realTimeDataList.stream().forEach(e -> {
                e.setLocalAddress(address);
                e.setCreateTime(new Date());
            });
            mqService.sendRealTimeData(realTimeDataList);
            realTimeDataList.clear();

        }
        sendPolice(mqService);
        sendRestore(mqService);
    }


    /**
     * 发送报警
     *
     * @param mqService
     * @return * @return null
     * @author wzb
     * @date 2023/6/19
     * @description
     */
    void sendPolice(MqService mqService) {
        if (!CollectionUtils.isEmpty(alarmEvents)) {
            mqService.sendPolice(alarmEvents);
            alarmEvents.clear();
        }
    }

    void sendRestore(MqService mqService) {
        if (!CollectionUtils.isEmpty(restore)) {
            mqService.sendRestore(restore);
            restore.clear();
        }
    }

}
